import axios from 'axios'
// import { useUserStore } from '@/stores'

const http = {
  respData: {
    method: 'GET',
    data: {},
    params: {},
    headers: {},
  },
  $axios(options = {}) {
    options.method = options.method || this.respData.method
    options.data = options.data || this.respData.data
    options.params = options.params || this.respData.params
    options.headers = options.headers || this.respData.headers

    // console.log('userStore.token :>> ', userStore.token)
    // const userStore = useUserStore()
    // if (!userStore.token) {
    //   console.log('用户未登陆')
    // }

    if (options.token) {
      options.headers.token = options.token
    }

    //加载动画,请求前
    return axios(options)
      .then((res) => {
        const bodyData = res?.data
        return new Promise((resolve, reject) => {
          if (!bodyData) reject()
          //请求后,关闭动画
          resolve(bodyData)
        })
      })
      .catch((err) => {
        console.log('err :>> ', err)
      })
  },
  get(url, params) {
    return axios.get(url, { params: params })
  },
  post(url, params) {
    return axios.post(url, params)
  },
}

export default http
